package cn.gaily.eduorder.controller;


import cn.gaily.commonutils.JwtUtils;
import cn.gaily.commonutils.R;
import cn.gaily.eduorder.entity.TOrder;
import cn.gaily.eduorder.service.TOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author xuchengwei
 * @since 2021-08-09
 */
@Api(description = "查询支付订单controller")
@RestController
@RequestMapping("/eduorder/t-order")
@CrossOrigin
public class TOrderController {
    @Autowired
    private TOrderService orderService;

    @ApiOperation(value = "生成订单接口")
    @PostMapping("/createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request){
        if(JwtUtils.checkToken(request)) {
            String orderNo = orderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
            return R.ok().data("orderId",orderNo);
        }else
            return R.error().data("msg","请登录");
    }
    @ApiOperation(value = "查询订单信息接口")
    @PostMapping("/getOrderInfo/{orderId}")
    public R getOrder(@PathVariable String orderId){
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        TOrder order = orderService.getOne(wrapper);
        return R.ok().data("item",order);
    }

    @ApiOperation(value = "根据课程id和用户id查询订单中订单状态")
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);//支付状态  1代表已支付

        int count = orderService.count(wrapper);
        if (count> 0) {
            return true;
        }else {
            return false;
        }
    }



}

